<?php

class UsersActions extends cfActions {
    public function beforeExecute()
    {
        $this->load->model('UserModel','users');
    }
    public function afterExecute()
    {
	}	

    public function executeIndex( $request )
    {
        redirect('users/list','refresh');
    }
    public function executeList( $request )
    {
		$this->session->unset_userdata('users._cf_ref', $this->getModuleUrl() . $this->input->server('QUERY_STRING'));
		$this->users_list_count = $this->users->count();
		
		// PAGER
		$this->per_page   = 5;
		$this->pagination = new cfPagination(array(
			'total_rows' => $this->users_list_count,
			'per_page'	 => $this->per_page,
			'page_query_string' => true
		));
		
		// END PAGER
		
		
		$query_start = (int) $this->input->get( $this->pagination->query_string_segment );
		
		$sort  = (int) $this->input->get( 'sort' );
		$order = (int) $this->input->get( 'order' );
		
		$fields = array( 'id_user','username', 'password');
		$orders  = array( 'asc','desc' );
		
		if( $sort > count( $fields ) -1 )
		{
			$sort = 0;
		}
		if( $order > count( $orders ) -1 )
		{
			$order = 0;
		}
		
		$this->users_list = $this->users->order_by($fields[$sort],$orders[$order])->get($this->per_page, $query_start )->all;
		
		$this->cf_page_title = 'Users Lists Table';
		
    }
    public function executeFilter( $request )
    {
        
    }
	public function executeUpdate( $request )
    {
		//exit;
		//if ($this->_enable_csrf == TRUE)
		//{
		//	$this->security->csrf_verify();
		//}
		
		$user = $this->users->where( 'id_user',$this->input->post('id_user') )
							->update( array(
								'username'  => $this->input->post('username'),
								'password'	=> $this->input->post('password') 
							));
		
		$this->session->set_userdata( 'users._cf_ref', $this->input->post('_cf_ref') );

		if( !$this->input->is_ajax_request() )
		{
			redirect( $this->getModuleName() . '/edit/'.$this->input->post('id_user')  );
		}
		else
		{
			
			$data = explode( '?',  $this->input->post('_cf_ref', '' ));
			$get  =  array();
			
			if( isset( $data[1]) )
			{
				$data = explode( '&', $data[1] );
				foreach( $data as $d ){
					$kv = explode( '=', $d );
					$get[$kv[0]] = $kv[1];
				}
			}
			
			
			return $this->forward( '', 'list', array( 0 ), array( $_POST, $get ) );
		}
    }
    public function executeEdit( $id )
    {
		$this->user =  $this->users->get_by_id_user( $id );
    }
    public function executeNew( $request )
    {

    }
    public function buildCriteria( $request )
    {

    }
    public function buildSort( $request )
    {

    }
    public function processForm( $request )
    {

    }
}
?>
